const loginIdValidator = new FieldValidator('txtLoginId', async function (val) {
    if (!val) {
        return '请填写账号'
    }
    const res = await API.exists(val)
    if (res.data) {
        return ("账号已存在，请重新填写")
    }
})
const nicknameValidator = new FieldValidator('txtNickname', function (val) {
    if (!val) {
        return '请填写昵称'
    }
})

const loginPwdValidator = new FieldValidator('txtLoginPwd', function (val) {
    if (!val) {
        return '请填写密码'
    }
})

const loginPwdComfirmValidator = new FieldValidator('txtLoginPwdConfirm', function (val) {
    if (!val) {
        return '请再次填写密码'
    }
    if (val !== loginPwdValidator.input.value) {
        return '两次密码不一致'
    }
})
const form = document.querySelector(".user-form")
form.onsubmit = async (e) => {
    e.preventDefault();
    const result = await FieldValidator.validate(loginIdValidator, nicknameValidator, loginPwdValidator, loginPwdComfirmValidator)
    if (!result) return
    const formData = new FormData(form)
    const data = Object.fromEntries(formData.entries())
    const res = await  API.reg(data)
    if(res.code === 0){
        alert("注册成功，点击确认进入登录页面")
        location.href = './login.html'
    }
}
